package com.zw.yzk.kabu.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.data.utils.PinyinUtils;
import com.zw.yzk.kabu.model.CityModel;
import com.zw.yzk.kabu.view.view.SelectCityView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/2/21.
 */

public class SelectCityPresenter implements Presenter {

    private SelectCityView selectCityView;

    @Inject
    public SelectCityPresenter() {

    }

    public void setView(SelectCityView selectCityView) {
        this.selectCityView = selectCityView;
    }


    public void loadCity(Context context) {
        selectCityView.loadCity(loadData(context));
    }

    public void filterCity(String key) {
        selectCityView.refreshSearchResult(filter(key));
    }

    /**
     * 加载城市列表
     *
     * @return 城市列表
     */
    private List<CityModel> loadData(Context context) {
        //TODO 多音字应该使用汉字码表查询解决，这里为了简单起见
        String[] citys = context.getResources().getStringArray(R.array.city_list);
        CityModel[] list = new CityModel[citys.length];
        for (int i = 0; i < citys.length; i++) {
            CityModel model = new CityModel();
            model.setName(citys[i]);
            switch (citys[i]) {
                case "长沙":
                    model.setSpells("CHANG#SHA");
                    model.setFirstSpell("C");
                    break;
                case "长治":
                    model.setSpells("CHANG#ZHI");
                    model.setFirstSpell("C");
                    break;
                case "长春":
                    model.setSpells("CHANG#CHUN");
                    model.setFirstSpell("C");
                    break;
                default:
                    model.setSpells(PinyinUtils.getPinYin(citys[i]));
                    model.setFirstSpell(model.getSpells().substring(0, 1));
                    break;
            }
            list[i] = model;
        }

        return Arrays.asList(list);
    }

    /**
     * 筛选搜索结果
     *
     * @param key 关键字
     * @return 结果列表
     */
    private List<CityModel> filter(String key) {
        List<CityModel> modelList = new ArrayList<>();
        String keySpell = PinyinUtils.getPinYin(key).toUpperCase();
        String shortKeySpell = getShortSpellFromPinYin(keySpell);
        boolean isCharOnly = isCharOnly(key);

        if (TextUtils.isDigitsOnly(key)) {
            return modelList;
        }
        for (CityModel model : selectCityView.getCityList()) {
            //匹配姓名、拼音和首字母缩写
            if (isCharOnly) {
                //匹配拼音、首字符
                String spell = model.getSpells().replace("#", "");
                String shortSpell = getShortSpellFromPinYin(model.getSpells());
                if (spell.contains(keySpell) || shortSpell.contains(shortKeySpell)) {
                    modelList.add(model);
                }
            } else {
                //匹配汉字
                if (model.getName().contains(key)) {
                    modelList.add(model);
                }
            }
        }
        return modelList;
    }

    /**
     * 判断字符串是否全为字母
     *
     * @param str 待判断字符串
     * @return true：字符串全是字母，false：字符串不是全为字母
     */
    private boolean isCharOnly(String str) {
        Pattern p = Pattern.compile("^[a-zA-Z]+");
        Matcher m = p.matcher(str);
        return m.matches();
    }


    /**
     * 从拼音中获取首字母缩写
     *
     * @param p 拼音
     * @return 结果
     */
    private String getShortSpellFromPinYin(String p) {
        StringBuilder builder = new StringBuilder();
        String[] s = p.split("#");
        if (s.length <= 1) {
            return p.toUpperCase();
        }
        for (String str : s) {
            builder.append(str.substring(0, 1));
        }
        return builder.toString().toUpperCase();
    }


    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {

    }
}
